﻿//===============================================================================
// File: DataAnnotationsModelMetadataProvider.cs
// Author:Veiller Hu
// CLR Version：4.0.30319.1
// Created Date:  2011/3/13 6:15:56 PM 
// Summary:  
//===============================================================================
//
// Copyright (c) East China Information Technology Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

namespace GPMS.Model
{
    public class FieldTemplateMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
        {
            DataAnnotationsModelMetadata result = (DataAnnotationsModelMetadata)base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

            string templateName = attributes.OfType<ITemplateField>()
                                            .Select(field => field.TemplateName)
                                            .LastOrDefault();
            List<System.Attribute> attributeList = new List<System.Attribute>(attributes);
            DisplayAttribute disp = attributeList.OfType<DisplayAttribute>().FirstOrDefault();
            if (disp != null)
            {
                result.ShortDisplayName = disp.Order.ToString();
                result.Description = disp.Description;
            }



            var data = (new FieldTemplateMetadata(disp, this, containerType, modelAccessor, modelType, propertyName, attributes.OfType<DisplayColumnAttribute>().FirstOrDefault(), attributes)

            {
                TemplateHint = !string.IsNullOrEmpty(templateName) ? templateName : result.TemplateHint,
                HideSurroundingHtml = result.HideSurroundingHtml,
                DataTypeName = result.DataTypeName,
                IsReadOnly = result.IsReadOnly,
                NullDisplayText = result.NullDisplayText,
                DisplayFormatString = result.DisplayFormatString,
                ConvertEmptyStringToNull = result.ConvertEmptyStringToNull,
                EditFormatString = result.EditFormatString,
                ShowForDisplay = result.ShowForDisplay,
                ShowForEdit = result.ShowForEdit,
                DisplayName = result.DisplayName,
                Description = result.Description,
                ShortDisplayName = result.ShortDisplayName,

            });
            return data;
        }
    }  

}
